package com.campus.help.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.help.common.Result;
import com.campus.help.entity.Banner;
import com.campus.help.service.BannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图控制器（小程序端）
 */
@Slf4j
@RestController
@RequestMapping("/banners")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 获取启用的轮播图列表（小程序端）
     * @param page 页码
     * @param size 每页大小
     * @return 轮播图列表
     */
    @GetMapping
    public Result<List<Banner>> getActiveBanners(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        
        try {
            log.info("小程序请求轮播图列表，页码：{}，每页大小：{}", page, size);
            
            // 创建分页参数
            Page<Banner> pageParam = new Page<>(page, size);
            
            // 查询启用的轮播图
            IPage<Banner> result = bannerService.getBannerPage(pageParam, null, 1);
            
            log.info("查询到轮播图数量：{}", result.getRecords().size());
            
            return Result.success(result.getRecords());
        } catch (Exception e) {
            log.error("获取轮播图列表失败", e);
            return Result.error("获取轮播图失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取轮播图详情
     * @param id 轮播图ID
     * @return 轮播图详情
     */
    @GetMapping("/{id}")
    public Result<Banner> getBannerById(@PathVariable Long id) {
        try {
            log.info("小程序请求轮播图详情，ID：{}", id);
            
            Banner banner = bannerService.getById(id);
            if (banner == null) {
                return Result.error("轮播图不存在");
            }
            
            // 检查轮播图是否启用
            if (banner.getStatus() != 1) {
                return Result.error("轮播图已禁用");
            }
            
            return Result.success(banner);
        } catch (Exception e) {
            log.error("获取轮播图详情失败，ID：{}", id, e);
            return Result.error("获取轮播图详情失败");
        }
    }

    /**
     * 获取轮播图统计信息
     * @return 统计信息
     */
    @GetMapping("/stats")
    public Result<Object> getBannerStats() {
        try {
            log.info("小程序请求轮播图统计信息");
            
            // 获取启用和禁用的轮播图数量
            Page<Banner> pageParam = new Page<>(1, 1000); // 获取所有数据用于统计
            
            IPage<Banner> activeResult = bannerService.getBannerPage(pageParam, null, 1);
            IPage<Banner> inactiveResult = bannerService.getBannerPage(pageParam, null, 0);
            
            return Result.success(new Object() {
                public final int totalCount = (int) (activeResult.getTotal() + inactiveResult.getTotal());
                public final int activeCount = (int) activeResult.getTotal();
                public final int inactiveCount = (int) inactiveResult.getTotal();
            });
        } catch (Exception e) {
            log.error("获取轮播图统计信息失败", e);
            return Result.error("获取统计信息失败");
        }
    }
}
